package com.hymjweb.demo.hymj.master_bill_manage.load.service;

import com.alibaba.fastjson.JSONObject;
import com.hymjweb.demo.SpringAopUtil;
import com.hymjweb.demo.framework.result.MIniUIGridResultMap;
import com.hymjweb.demo.framework.sys.BaseService;
import com.hymjweb.demo.framework.sys.SysConstant;
import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.framework.util.DoubleUtil;
import com.hymjweb.demo.hymj.log_manage.aop.annotation.OperationLogger;
import com.hymjweb.demo.hymj.master_bill_manage.auto.bean.BMasterBillAutoPojo;
import com.hymjweb.demo.hymj.master_bill_manage.auto.service.BMasterBillAutoService;
import com.hymjweb.demo.hymj.master_bill_manage.flight.bean.BMasterBillFlightPojo;
import com.hymjweb.demo.hymj.master_bill_manage.flight.service.BMasterBillFlightService;
import com.hymjweb.demo.hymj.master_bill_manage.load.bean.BNativeBillLoadPojo;
import com.hymjweb.demo.hymj.master_bill_manage.load.mapper.BNativeBillLoadMapper;
import com.hymjweb.demo.hymj.master_bill_manage.pull.bean.BMasterBillPullPojo;
import com.hymjweb.demo.hymj.master_bill_manage.pull.detail.bean.BMasterBillPullDetailPojo;
import com.hymjweb.demo.hymj.master_bill_manage.pull.detail.service.BMasterBillPullDetailService;
import com.hymjweb.demo.hymj.master_bill_manage.pull.service.BMasterBillPullService;
import com.hymjweb.demo.hymj.native_bill_manage.bean.BNativeBillPojo;
import com.hymjweb.demo.hymj.native_bill_manage.service.BNativeBillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class BNativeBillLoadService extends BaseService {

    @Autowired
    private BNativeBillLoadMapper bNativeBillLoadMapper;

    @Autowired
    private BNativeBillService bNativeBillService;

    @Autowired
    private BMasterBillPullService bMasterBillPullService;

    @Autowired
    private BMasterBillFlightService bMasterBillFlightService;

    @Autowired
    private BMasterBillAutoService bMasterBillAutoService;

    @Autowired
    private BMasterBillPullDetailService bMasterBillPullDetailService;

    /**
     * 根据配置后的分单列表，获取配载后的分单对象列表
     *
     * @param bNativeBillLoadPojos
     * @return
     */
    public List<BNativeBillPojo> getLoadBNativeBillPojoByLoadList(List<BNativeBillLoadPojo> bNativeBillLoadPojos) throws Exception {
        if (bNativeBillLoadPojos == null || bNativeBillLoadPojos.isEmpty()) {
            throwException("配载分单列表为空，根据配载分单列表获取原始分单列为失败!");
        }

        List<BNativeBillPojo> bNativeBillPojos = new ArrayList<>();
        for (BNativeBillLoadPojo loadBillPojo : bNativeBillLoadPojos) {
            BNativeBillPojo nativeBillPojo = bNativeBillService.getBNativeBillPojoById(loadBillPojo.getNativeBillId());
            // 一定要 clone 出一个新的对象，否则如 ID 同时查询出来的对象是同一个对象
            BNativeBillPojo cloneNativeBillPojo = (BNativeBillPojo) nativeBillPojo.clone();
            // 更新为配载后的值
            // 分单航班件数
            cloneNativeBillPojo.setTotalNumber(DataTypeUtil.toInteger(loadBillPojo.getLoadNumber().toString()));
            // 分单航班实重
            cloneNativeBillPojo.setActualWeight(loadBillPojo.getLoadActualWeight());
            // 航班分单计重
            cloneNativeBillPojo.setChargeableWeight(loadBillPojo.getLoadChargeableWeight());
            // 航班分单体积
            cloneNativeBillPojo.setVolume(loadBillPojo.getLoadVolume());

            /**
             * airlines_id
             * flight_no
             * flight_name
             * flight_date
             */
            // 修改航班为配载后的航班
            cloneNativeBillPojo.setAirlinesId(loadBillPojo.getAirlinesId());

            cloneNativeBillPojo.setFlightNo(loadBillPojo.getFlightNo());

            cloneNativeBillPojo.setFlightName(loadBillPojo.getFlightName());

            cloneNativeBillPojo.setFlightDate(loadBillPojo.getFlightDate());

            // 这个要设置为null，否则前端解析时会出问题，在编辑或查看时不需要使用这个字段，所以直接设置为null
            cloneNativeBillPojo.setShareCostComment(null);

            // 加入集合
            bNativeBillPojos.add(cloneNativeBillPojo);
        }
        return bNativeBillPojos;
    }


    /**
     * 查询航空主单包含的已经配载的分单
     *
     * @param param
     * @return
     */
    @Transactional(rollbackFor = {Exception.class})
    public Object queryNativeBillList(Map param) {
        this.setQueryCommParam(param);
        this.pageListInit(param);
        List<BNativeBillLoadPojo> list = queryNativeBillListNoPage(param);
        MIniUIGridResultMap resultMap = this.pageToGrid(list);
        return resultMap;
    }

    /**
     * 根据参数查询配载分单信息，无分页
     *
     * @param param
     * @return
     */
    public List<BNativeBillLoadPojo> queryNativeBillListNoPage(Map param) {
        return bNativeBillLoadMapper.findAllLoad(param);
    }

    /**
     * 根据主单号删除配载信息,这个地方也会有对分单配载信息的修正
     *
     * @param masterBillId
     * @return
     */
    @Transactional(rollbackFor = {Exception.class})
    public int deleteLoadNativeBillByMasterBillId(String masterBillId) throws Exception {
        if (DataTypeUtil.isEmptyStr(masterBillId)) {
            throwException("删除主单参数错误!");
        }
        // 先根据主单号查询到删除之前的配载信息
        // 需要对比现在配载的分单与之前分单的区别
        // 1.删除某个主单包含的分单之前需要把这个主单下原来的分单是否配载完成的状态改为部分配载与未实配
        Map<String, Object> param = new HashMap<>();
        param.put("masterBillId", masterBillId);
        List<BNativeBillLoadPojo> deleteBeforeList = queryNativeBillListNoPage(param);

        if (deleteBeforeList != null && !deleteBeforeList.isEmpty()) {
            for (BNativeBillLoadPojo loadPojo : deleteBeforeList) {
                String nativeBillId = loadPojo.getNativeBillId();
                // 根据分单ID获取分单对象
                BNativeBillPojo bNativeBillPojo = bNativeBillService.getBNativeBillPojoById(nativeBillId);

                if (loadPojo.getLoadNumber().longValue() == bNativeBillPojo.getTotalNumber().longValue()) {
                    // 如果等，则直接改为未配载
                    bNativeBillService.updateLoadStatus(nativeBillId, SysConstant.XT_DIC_BILL_LOAD_STATUS.NO_LOAD.getId());
                } else if (loadPojo.getLoadNumber().longValue() < bNativeBillPojo.getTotalNumber().longValue()) {
                    // 如果小于原来的分单件数，则修改为部分配载
                    bNativeBillService.updateLoadStatus(nativeBillId, SysConstant.XT_DIC_BILL_LOAD_STATUS.PART_LOAD.getId());
                }
            }
        }

        return bNativeBillLoadMapper.deleteLoadNativeBill(masterBillId);
    }


    public BNativeBillLoadPojo queryBNativeBillLoadPojoByMapParam(Map param) throws Exception {
        if (param == null || param.isEmpty()) {
            throwException("获取分单配载对象参数错误");
        }
        return bNativeBillLoadMapper.queryBNativeBillLoadPojoByMapParam(param);
    }

    /**
     * 批量插入配载分单号,注意有状态修正
     *
     * @param bNativeBillLoadPojoList
     * @return
     */
    @Transactional(rollbackFor = {Exception.class})
    public int batchInsertLoadBill(List<BNativeBillLoadPojo> bNativeBillLoadPojoList) throws Exception {

        if (bNativeBillLoadPojoList == null || bNativeBillLoadPojoList.isEmpty()) {
            throwException("批量插入分单配载信息参数错误!");
        }

        // 批量插入前要做UK(master_bill_id,master_bill_type_id,native_bill_id,flight_name,flight_date)
        boolean ukFlag = false;

        // 针对每个分单做 UK 检测
        for (BNativeBillLoadPojo loadPojo : bNativeBillLoadPojoList) {
            Map<String, Object> param = new HashMap<>();
            param.put("masterBillId", loadPojo.getMasterBillId());
            param.put("masterBillTypeId", loadPojo.getMasterBillTypeId());
            param.put("nativeBillId", loadPojo.getNativeBillId());
            param.put("flightName", loadPojo.getFlightName());
            param.put("flightDate", loadPojo.getFlightDate());
            BNativeBillLoadPojo tempPojo = this.queryBNativeBillLoadPojoByMapParam(param);
            if (tempPojo != null) {
                ukFlag = true;
                break;
            }
        }

        if (ukFlag) {
            throwException("配载表【主单号、主单类型、分单号、分单班次、分单日期】出现重复配载信息!");
        }

        // 批量插入
        int effectRows = insertLoadBillBatch(bNativeBillLoadPojoList);

        return effectRows;
    }

    @Transactional(rollbackFor = {Exception.class})
    public int insertLoadBillBatch(List<BNativeBillLoadPojo> bNativeBillLoadPojoList) throws Exception {
        if (bNativeBillLoadPojoList == null || bNativeBillLoadPojoList.isEmpty()) {
            throwException("主单配载为空，主单配载新增失败!");
        }
        int affectRows = 0;
        for (BNativeBillLoadPojo pojo : bNativeBillLoadPojoList) {
            affectRows += SpringAopUtil.getBean(this.getClass()).insert(pojo);
        }
        return affectRows;
    }

    //@OperationLogger(oper = SysConstant.OPER_ADD, operTitle = "主单配载-添加", operMemo = "", classType = BMasterBillFlightPojo.class)
    @Transactional(rollbackFor = {Exception.class})
    public int insert(BNativeBillLoadPojo pojo) throws Exception {
        if (pojo == null) {
            throwException("主单配载对象为空，主单配载添加失败!");
        }
        return bNativeBillLoadMapper.insert(pojo);
    }

    @OperationLogger(oper = SysConstant.OPER_UPDATE, operTitle = "主单配载-更新", operMemo = "", classType = BNativeBillLoadPojo.class)
    @Transactional(rollbackFor = {Exception.class})
    public int update(BNativeBillLoadPojo pojo) throws Exception {
        if (pojo == null) {
            throwException("主单配载对象为空，主单配载更新失败!");
        }
        return bNativeBillLoadMapper.update(pojo);
    }

    /**
     * 在分单配载表中获取对应分单已经配载的信息
     *
     * @param billId
     * @return
     */
    public Map<String, Object> getLoadTotalNativeMap(String billId) {
        return bNativeBillLoadMapper.getLoadTotalNativeMap(billId);
    }

    /**
     * 根据主单批量获取分单配载信息列表
     *
     * @param masterBillIds
     * @return
     */
    public List<BNativeBillLoadPojo> queryNativeBillListByInMasterBillId(String masterBillIds) {
        if (DataTypeUtil.isEmptyStr(masterBillIds)) {
            return null;
        }
        return bNativeBillLoadMapper.queryNativeBillListByInMasterBillId(masterBillIds);
    }

    /**
     * 根据主单获取分单配载信息列表
     *
     * @param masterBillId
     * @return
     */
    public List<BNativeBillLoadPojo> queryNativeBillListByMasterBillId(String masterBillId) {
        if (DataTypeUtil.isEmptyStr(masterBillId)) {
            return null;
        }
        return bNativeBillLoadMapper.queryNativeBillListByMasterBillId(masterBillId);
    }

    /**
     * @param param
     * @return
     */
    public Object getPageList(Map param) {
        // 设置公共查询参数,加上部门
        this.setQueryCommParam(param);
        this.pageListInit(param);
        List<Map<String, Object>> list = getNoPageList(param);
        // 如果需要进行驼峰转换是不能产生新对象的
        MIniUIGridResultMap resultMap = this.pageToGrid(list);
        return resultMap;
    }


    public Object queryNativeBillLoadProfit(Map param) {
        this.pageListInit(param);
        List<Map<String, Object>> list = bNativeBillService.getNoPageList(param);
        // 如果需要进行驼峰转换是不能产生新对象的
        MIniUIGridResultMap resultMap = this.pageToGrid(list);
        return resultMap;
    }


    public List<Map<String, Object>> getNoPageList(Map param) {
        List<Map<String, Object>> list = bNativeBillLoadMapper.getNoPageList(param);
        return list;
    }

    /**
     * 分单成本利润核算
     *
     * @param bNativeBillLoadPojo
     * @return
     * @throws Exception
     */
    @Transactional(rollbackFor = {Exception.class})
    public int loadNativeBillCostAccounting(BNativeBillLoadPojo bNativeBillLoadPojo,Integer costShareStandard) throws Exception {

        if (bNativeBillLoadPojo == null || costShareStandard == null) {
            throwException("待核算成本配载分单不存在或核算分单成本标准不存在!");
        }

        // 1 定义主单位核算成本信息
        // 1.1 核算件数
        Long masterBillAccountingTotalNumber = 0L;
        // 1.2 核算运费
        Double masterBillAccountingTransportCost = 0D;
        // 1.3 核算地面操作费
        Double masterBillAccountingGroundAmount = 0D;
        // 1.4 核算航空附加费
        Double masterBillAccountingAirlinesAddCost = 0D;
        // 1.5 核算其他费用
        Double masterBillAccountingOtherCharges = 0D;
        // 1.6 核算计费重量
        Double masterBillAccountingActualLoadTotalWeight = 0D;

        // 2 定义主单原始成本
        // 2.1 主单总件数
        Long masterBillTotalNumber = 0L;
        // 2.2 主单运费
        Double masterBillTransportCost = 0D;
        // 2.3 主单地面运费
        Double masterBillGroundAmount = 0D;
        // 2.4 主单航空附加费
        Double masterBillAirlinesAddCost = 0D;
        // 2.5 主单其他费用
        Double masterBillOtherCharges = 0D;
        // 2.7 主单实际配载总重量
        Double masterBillActualLoadTotalWeight = 0D;


        // 3 主单拉货对象
        BMasterBillPullPojo bMasterBillPullPojo = null;
        // 3.1 根据配载的主单类型获取对应的主单对象
        if (bNativeBillLoadPojo.getMasterBillTypeId().longValue() == SysConstant.XT_DIC_MASTER_BILL_TYPE.FLIGHT.getId()) {
            BMasterBillFlightPojo bMasterBillFlightPojo = bMasterBillFlightService.getBMasterBillFlightPojoById(bNativeBillLoadPojo.getMasterBillId());
            if (bMasterBillFlightPojo == null) {
                throwException("待核算成本配载分单对应的的主单(空运)不存在!");
            }
            masterBillTotalNumber = bMasterBillFlightPojo.getTotalNumber();
            masterBillTransportCost = bMasterBillFlightPojo.getTransportCost();
            masterBillGroundAmount = bMasterBillFlightPojo.getGroundAmount();
            masterBillAirlinesAddCost = bMasterBillFlightPojo.getAirlinesAddCost();
            masterBillOtherCharges = bMasterBillFlightPojo.getOtherCharges();
            masterBillActualLoadTotalWeight = bMasterBillFlightPojo.getActualLoadTotalWeight();

            // 根据主单号取出对应的拉货主对象
            bMasterBillPullPojo = bMasterBillPullService.getBMasterBillPullPojoByMasterBillIdAndBillTypeId(bMasterBillFlightPojo.getId(), SysConstant.XT_DIC_MASTER_BILL_TYPE.FLIGHT.getId());

        } else {
            BMasterBillAutoPojo bMasterBillAutoPojo = bMasterBillAutoService.getBMasterBillAutoPojoById(bNativeBillLoadPojo.getMasterBillId());
            if (bMasterBillAutoPojo == null) {
                throwException("待核算成本配载分单对应的的主单(汽运)不存在!");
            }
            masterBillTotalNumber = bMasterBillAutoPojo.getTotalNumber();
            masterBillTransportCost = bMasterBillAutoPojo.getFreightAmount();
            masterBillGroundAmount = 0D;
            masterBillAirlinesAddCost = 0D;
            masterBillOtherCharges = bMasterBillAutoPojo.getOtherCharges();
            masterBillActualLoadTotalWeight = bMasterBillAutoPojo.getActualLoadTotalWeight();


            // 根据主单号取出对应的拉货主对象
            bMasterBillPullPojo = bMasterBillPullService.getBMasterBillPullPojoByMasterBillIdAndBillTypeId(bMasterBillAutoPojo.getId(), SysConstant.XT_DIC_MASTER_BILL_TYPE.AUTO.getId());
        }

        if (bMasterBillPullPojo != null) {
            // 设定拉货后参与核算的成本信息
            masterBillAccountingTotalNumber = masterBillTotalNumber - bMasterBillPullPojo.getPullDisposeNumber();
            masterBillAccountingTransportCost = DoubleUtil.sum(masterBillTransportCost, bMasterBillPullPojo.getPullDisposeTransportCostAmount(), 2);
            masterBillAccountingGroundAmount = DoubleUtil.sum(masterBillGroundAmount, bMasterBillPullPojo.getPullDisposeGroundAmount(), 2);
            masterBillAccountingAirlinesAddCost = DoubleUtil.sum(masterBillAirlinesAddCost, bMasterBillPullPojo.getPullDisposeAirlinesAddCostAmount(), 2);
            masterBillAccountingOtherCharges = DoubleUtil.sum(masterBillOtherCharges, bMasterBillPullPojo.getPullDisposeOtherChargesAmount(), 2);
            masterBillAccountingActualLoadTotalWeight = DoubleUtil.sub(masterBillActualLoadTotalWeight, bMasterBillPullPojo.getPullDisposeActualWeight(), 2);

            // 获取对应的分单拉货明细
            Map<String, Object> param = new HashMap<>();
            param.put("masterBillPullId", bMasterBillPullPojo.getId());
            param.put("nativeBillId", bNativeBillLoadPojo.getNativeBillId());
            List<BMasterBillPullDetailPojo> detailPojoList = bMasterBillPullDetailService.queryPullDetailListNoPage(param);

            if (detailPojoList == null || detailPojoList.isEmpty() || detailPojoList.size() > 1) {
                throwException("拉货分单配载成本核算获取拉货分单明细错误!");
            }

            BMasterBillPullDetailPojo detailPojo = detailPojoList.get(0);

            // 如果是部分拉货补发，因为没有产生新的主单，所以不能减去对应的重量，直接使用原主单的重量就好
            if (masterBillTotalNumber.longValue() != detailPojo.getPullNumber().longValue() && detailPojo.getPullDisposeId().longValue() == SysConstant.XT_DIC_NATIVE_PULL_DISPOSE.SUPPLY.getId()) {
                masterBillAccountingActualLoadTotalWeight = masterBillActualLoadTotalWeight;
            }
        } else {
            masterBillAccountingTotalNumber = masterBillTotalNumber;
            masterBillAccountingTransportCost = masterBillTransportCost;
            masterBillAccountingGroundAmount = masterBillGroundAmount;
            masterBillAccountingAirlinesAddCost = masterBillAirlinesAddCost;
            masterBillAccountingOtherCharges = masterBillOtherCharges;
            masterBillAccountingActualLoadTotalWeight = masterBillActualLoadTotalWeight;
        }

        // 主单计费重量为0
        if (costShareStandard.intValue() == SysConstant.COST_SHARE_STANDARD_WEIGHT.intValue()) {
            if(masterBillAccountingActualLoadTotalWeight <= 0){
                throwException("主单【" + bNativeBillLoadPojo.getMasterBillId() + "】计费重量为0，不能对主单下配载的分单进行成本分摊核算!");
            }
        }
        else{
            if(masterBillAccountingTotalNumber <= 0){
                throwException("主单【" + bNativeBillLoadPojo.getMasterBillId() + "】件数为0，不能对主单下配载的分单进行成本分摊核算!");
            }
        }

        // 配载对象进行成本核算
        this.accountingCost(bNativeBillLoadPojo, costShareStandard ,masterBillAccountingTotalNumber, masterBillAccountingTransportCost, masterBillAccountingGroundAmount, masterBillAccountingAirlinesAddCost, masterBillAccountingOtherCharges, masterBillAccountingActualLoadTotalWeight);

        // 更新配载分单成本信息
        return this.update(bNativeBillLoadPojo);
    }

    /**
     * @param bNativeBillLoadPojo                  待核算成本对象
     * @param masterBillAccountingTotalNumber      主单调整后的剩余总件数
     * @param masterBillAccountingTransportCost    主单调整后的航空运费
     * @param masterBillAccountingGroundAmount     主单调整后的地面运费
     * @param masterBillAccountingAirlinesAddCost  主单调整后的航空附加费
     * @param masterBillAccountingOtherCharges     主单调整后的其他费用
     * @param masterBillAccountingActualLoadTotalWeight 主单调整后的实际配载重量
     */

    public void accountingCost(BNativeBillLoadPojo bNativeBillLoadPojo,Integer costShareStandard , Long masterBillAccountingTotalNumber, Double masterBillAccountingTransportCost, Double masterBillAccountingGroundAmount, Double masterBillAccountingAirlinesAddCost, Double masterBillAccountingOtherCharges, Double masterBillAccountingActualLoadTotalWeight) throws Exception {
        // 取出配载件数
        Long loadNumber = bNativeBillLoadPojo.getLoadNumber();
        // 1.取出配载分单的计费重量
        Double loadActualWeight = bNativeBillLoadPojo.getLoadActualWeight();
        // 2.得到配载重量点主单重量的百分比
        // 2.1 这个地方要考虑拉货时对应的调整后的分单全部拉货，主单也全部拉货
        Double  actualLoadWeightPercent = 0D;
        // 2.2 航空运费配载成本
        Double loadTransportCost = 0D;
        // 2.3 地面操作费配载成本
        Double loadGroundCost = 0D;
        // 2.4 主单附加费配载成本
        Double loadAirlinesAddCost = 0D;
        // 2.5 其他费用配载成本
        Double loadOtherChargesCost = 0D;

        if(costShareStandard.intValue() == SysConstant.COST_SHARE_STANDARD_WEIGHT.intValue()){
            if(masterBillAccountingActualLoadTotalWeight <= 0D){
                throwException("主单实际配载重量不大于零，不能进行配载分单成本与利润核算!");
            }
            actualLoadWeightPercent = DoubleUtil.div(loadActualWeight, masterBillAccountingActualLoadTotalWeight, 2);
        }
        else{
            if(masterBillAccountingTotalNumber <= 0){
                throwException("主单件数不大于零，不能进行配载分单成本与利润核算!");
            }
            actualLoadWeightPercent = DoubleUtil.div(loadNumber*1.0, masterBillAccountingTotalNumber*1.0, 2);
        }

        // 2.6.计算对应的配载成本
        // 2.6.1 航空运费配载成本
        loadTransportCost = DoubleUtil.mul(masterBillAccountingTransportCost, actualLoadWeightPercent, 2);
        // 2.6.2 地面操作费配载成本
        loadGroundCost = DoubleUtil.mul(masterBillAccountingGroundAmount, actualLoadWeightPercent, 2);
        // 2.6.3 主单附加费配载成本
        loadAirlinesAddCost = DoubleUtil.mul(masterBillAccountingAirlinesAddCost, actualLoadWeightPercent, 2);
        // 2.6.4 其他费用配载成本
        loadOtherChargesCost = DoubleUtil.mul(masterBillAccountingOtherCharges, actualLoadWeightPercent, 2);

        // 3 分摊总成本
        Double loadTotalCost = DoubleUtil.round2(loadTransportCost + loadGroundCost + loadAirlinesAddCost + loadOtherChargesCost);

        bNativeBillLoadPojo.setShareTransportCostAmount(loadTransportCost);
        bNativeBillLoadPojo.setShareGroundAmount(loadGroundCost);
        bNativeBillLoadPojo.setShareAirlinesAddCostAmount(loadAirlinesAddCost);
        bNativeBillLoadPojo.setShareOtherChargesAmount(loadOtherChargesCost);
        bNativeBillLoadPojo.setShareTotalCostAmount(loadTotalCost);

        List<Map<String, Object>> shareCommentList = new ArrayList<>();
        Map<String, Object> masterShareCommentMap = new HashMap<>();
        masterShareCommentMap.put("主单号", bNativeBillLoadPojo.getMasterBillId());
        masterShareCommentMap.put("主单待分摊运费", masterBillAccountingTransportCost);
        masterShareCommentMap.put("主单待分摊地面操作费", masterBillAccountingGroundAmount);
        masterShareCommentMap.put("主单待分摊附加费", masterBillAccountingAirlinesAddCost);
        masterShareCommentMap.put("主单待分摊其他费用", masterBillAccountingOtherCharges);
        masterShareCommentMap.put("主单待分摊配载总重量", masterBillAccountingActualLoadTotalWeight);

        Map<String, Object> nativeShareCommentMap = new HashMap<>();
        nativeShareCommentMap.put("分单号", bNativeBillLoadPojo.getNativeBillId());


        if(costShareStandard.intValue() == SysConstant.COST_SHARE_STANDARD_WEIGHT.intValue()){
            nativeShareCommentMap.put("配载实重", bNativeBillLoadPojo.getLoadActualWeight());
            nativeShareCommentMap.put("配载实重占主单实际配载总重百分比", DoubleUtil.mul(actualLoadWeightPercent, 100, 2) + "%");
        }
        else{
            nativeShareCommentMap.put("配载件数", bNativeBillLoadPojo.getLoadNumber());
            nativeShareCommentMap.put("配载件数占主单件数百分比", DoubleUtil.mul(actualLoadWeightPercent, 100, 2) + "%");
        }
        nativeShareCommentMap.put("配载分摊主单运费成本", loadTransportCost);
        nativeShareCommentMap.put("配载分摊主单地面操作费用成本", loadGroundCost);
        nativeShareCommentMap.put("配载分摊主单附加费用成本", loadAirlinesAddCost);
        nativeShareCommentMap.put("配载分摊主单其他费用成本", loadOtherChargesCost);
        nativeShareCommentMap.put("配载分摊总成本", loadTotalCost);

        shareCommentList.add(masterShareCommentMap);
        shareCommentList.add(nativeShareCommentMap);

        bNativeBillLoadPojo.setShareTotalCostComment(JSONObject.toJSONString(shareCommentList));
    }


    @Transactional(rollbackFor = {Exception.class})
    public int delete(String id) throws Exception {
        if (DataTypeUtil.isEmptyStr(id)) {
            throwException("删除分单配载对象参数错误!");
        }
        return bNativeBillLoadMapper.delete(id);
    }

    public BNativeBillLoadPojo queryBNativeBillLoadPojoById(String id) throws Exception {
        if (DataTypeUtil.isEmptyStr(id)) {
            throwException("分单配载对象查询参数错误!");
        }
        return bNativeBillLoadMapper.queryBNativeBillLoadPojoById(id);
    }




}
